﻿using BW.WMS.CodeEnum;
using BW.WMS.Model;

using BW.WMS.StoreManagement_XKY.DTO.Input;
using BW.WMS.StoreManagement_XKY.DTO.Output;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace BW.WMS.StoreManagement_XKY
{
    public class ItemAppService:ApplicationService,IItemAppService
    {
        public IRepository<Dm_Item> _itemRepositoey;
        public IRepository<Dm_ItemType> _itemTypeRepositoey;
        public IRepository<Dm_Unit> _unitRepositoey;
        public IRepository<Dm_Client> _clientRepositoey;
        public IRepository<Dm_Ent> _entRepository;
        public ItemAppService(IRepository<Dm_Item> itemRepositoey, IRepository<Dm_ItemType> itemTypeRepositoey, IRepository<Dm_Unit> unitRepositoey, IRepository<Dm_Client> clientRepositoey, IRepository<Dm_Ent> entRepository)
        {
            _itemRepositoey = itemRepositoey;
            _itemTypeRepositoey = itemTypeRepositoey;
            _unitRepositoey = unitRepositoey;
            _clientRepositoey = clientRepositoey;
            _entRepository = entRepository;
        }

        public async Task<ApiResult<int>> Add(ItemInput input)
        {
            input.Id = Guid.NewGuid();
            input.CreationTime = DateTime.Now;
            input.CreatorId = Guid.NewGuid();
            var data = ObjectMapper.Map<ItemInput, Dm_Item>(input);
            await _itemRepositoey.InsertAsync(data);
            return new ApiResult<int>()
            {
                Code = ApiEnum.Success,
                Message = "添加成功",
                Data = 1
            };
            
        }

        public async Task<ApiResult<int>> Delete(string itemid)
        {
            var item = await _itemRepositoey.GetAsync(x=>x.Itemid==itemid);
            await _itemRepositoey.DeleteAsync(item);
            return new ApiResult<int>()
            {
                Code = ApiEnum.Success,
                Message = "删除成功",
                Data = 1
            };
        }

        public async Task<ApiPaging<ItemOutput>> GetPage(int pageIndex, int pageSize, string? Itemid, string? Itemname)
        {
            var item = await _itemRepositoey.GetQueryableAsync();
            var itemtype = await _itemTypeRepositoey.GetQueryableAsync();
            var unit = await _unitRepositoey.GetQueryableAsync();
            var client = await _clientRepositoey.GetQueryableAsync();
            var ent = await _entRepository.GetQueryableAsync();
            if (!string.IsNullOrEmpty(Itemid)) 
            {
                item = item.Where(x => x.Itemid.Contains(Itemid));
            }
            if (!string.IsNullOrEmpty(Itemname)) 
            {
                item = item.Where(x => x.Itemname.Contains(Itemname));
            }
            var data = from a in item
                       join b in itemtype on a.Itemtypeid equals b.Itemtypeid
                       join c in unit on a.Unitid equals c.Unitid
                       join d in client on a.Clientid equals d.Clientid
                       join e in ent on a.Entid equals e.Entid
                       select new ItemOutput
                       {
                           Id=a.Id,
                           Itemid = a.Itemid,
                           Itemname = a.Itemname,
                           Satus = a.Satus,
                           Itemtypeid = a.Itemtypeid,
                           ItemtypeName = b.Itemtypename,
                           Itemspec = a.Itemspec,
                           Unitid = a.Unitid,
                           UnitName = c.Unitname,
                           Entid = a.Entid,
                           EntName = e.Entname,
                           Clientid = a.Clientid,
                           ClientName = d.Clientname,
                           Purchaseprice = a.Purchaseprice,
                           Outboundprice = a.Outboundprice
                       };
            return new ApiPaging<ItemOutput>()
            {
                TotalCount=data.Count(),
                TotalPages = (int)Math.Ceiling(data.Count() * 1.0 / pageSize),
                PageData = data.Skip((pageIndex-1)*pageSize).Take(pageSize).ToList()
            };
        }

        public async Task<ApiPaging<ItemOutput>> GetList()
        {
            var item=await _itemRepositoey.GetQueryableAsync();
            var itemtype=await _itemTypeRepositoey.GetQueryableAsync();
            var unit=await _unitRepositoey.GetQueryableAsync();
            var client=await _clientRepositoey.GetQueryableAsync();
            var ent=await _entRepository.GetQueryableAsync();

            var data = from a in item
                       join b in itemtype on a.Itemtypeid equals b.Itemtypeid
                       join c in unit on a.Unitid equals c.Unitid
                       join d in client on a.Clientid equals d.Clientid
                       join e in ent on a.Entid equals e.Entid
                       select new ItemOutput
                       {
                           Id=a.Id,
                           Itemid = a.Itemid,
                           Itemname = a.Itemname,
                           Satus = a.Satus,
                           Itemtypeid = a.Itemtypeid,
                           ItemtypeName = b.Itemtypename,
                           Itemspec = a.Itemspec,
                           Unitid = a.Unitid,
                           UnitName = c.Unitname,
                           Entid = a.Entid,
                           EntName = e.Entname,
                           Clientid = a.Clientid,
                           ClientName = d.Clientname,
                           Purchaseprice = a.Purchaseprice,
                           Outboundprice = a.Outboundprice
                       };
            return new ApiPaging<ItemOutput>()
            {
                PageData = data.ToList()
            };
        }

        public async Task<ApiResult<int>> Update(ItemInput input)
        {
            input.LastModificationTime = DateTime.Now;
            var data =await _itemRepositoey.GetAsync(x => x.Itemid == input.Itemid);
            data=ObjectMapper.Map(input,data);
            await _itemRepositoey.UpdateAsync(data);
            return new ApiResult<int>()
            {
                Code = ApiEnum.Success,
                Message = "修改成功",
                Data = 1
            };
        }

        public async Task<ApiResult<List<ClientOutput>>> GetClient()
        {
            var data=await _clientRepositoey.GetListAsync();
            var result = ObjectMapper.Map<List<Dm_Client>, List<ClientOutput>>(data);
            return new ApiResult<List<ClientOutput>>()
            {
                Code = ApiEnum.Success,
                Data = result,
                Message = "查询成功"
            };
        }

        public async Task<ApiResult<List<ItemTyepOutput>>> GetItemType()
        {
            var data=await _itemTypeRepositoey.GetListAsync();
            var result = ObjectMapper.Map<List<Dm_ItemType>, List<ItemTyepOutput>>(data);
            return new ApiResult<List<ItemTyepOutput>>()
            {
                Code = ApiEnum.Success,
                Data = result,
                Message = "查询成功"
            };
        }
    }
}
